package cz.semjobKB.gui;

import com.vaadin.server.FileDownloader;
import com.vaadin.server.FileResource;
import com.vaadin.server.Resource;
import com.vaadin.ui.*;
import cz.semjobKB.virtuoso.document.api.IDocumentDetail;
import org.apache.commons.io.FilenameUtils;

import javax.annotation.PostConstruct;
import java.io.File;

/**
 * Displays statistics, triples and terminology of a document
 */
@SuppressWarnings("restriction")
public class DocumentInfo extends VerticalSplitPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@PostConstruct
    protected void init() {		
		
	}
	
	protected ComponentContainer fillDocumentInfo(IDocumentDetail docDetail) {
		
		this.setSplitPosition(30);
		this.setLocked(true);
		
		VerticalLayout verticalLayout = new VerticalLayout();
		verticalLayout.addComponent(new Label(docDetail.getDocumentName()));
		
		HorizontalLayout docFileLayout = new HorizontalLayout();
		docFileLayout.addComponent(new Label("Document File: \t"));
		Button save = new Button("Download");
		Resource res = new FileResource(new File(docDetail.getDocumentPath()));
		FileDownloader fd = new FileDownloader(res);
		fd.extend(save);
		docFileLayout.addComponent(save);
		verticalLayout.addComponent(docFileLayout);
		
		HorizontalLayout docTextLayout = new HorizontalLayout();
		docTextLayout.addComponent(new Label("Document Text: \t"));
		
		Button textButton = new Button("Download");
		File documentFile = new File(docDetail.getDocumentPath());
		String path = documentFile.getParent() + File.separator + "txt" + File.separator + FilenameUtils.removeExtension(documentFile.getName()) + ".txt";
		Resource resource = new FileResource(new File(path));
		FileDownloader fileDownloader = new FileDownloader(resource);
		fileDownloader.extend(textButton);
		docTextLayout.addComponent(textButton);
		verticalLayout.addComponent(docFileLayout);
        
        verticalLayout.addComponent(docTextLayout);
        
        VerticalLayout statistics = new VerticalLayout();
        HorizontalLayout tripleNumLayout = new HorizontalLayout();
        tripleNumLayout.addComponent(new Label("Triples count: \t"));
        tripleNumLayout.addComponent(new Label(docDetail.getTriplesCount().toString()));
        statistics.addComponent(tripleNumLayout);
        
        HorizontalLayout termNumLayout = new HorizontalLayout();
        termNumLayout.addComponent(new Label("Unique terms count: \t"));
        termNumLayout.addComponent(new Label(docDetail.getUniqueTermCount().toString()));
        statistics.addComponent(termNumLayout);
        
        HorizontalLayout predNumLayout = new HorizontalLayout();
        predNumLayout.addComponent(new Label("Unique predicates count: \t"));
        predNumLayout.addComponent(new Label(docDetail.getUniquePredicateCount().toString()));
        statistics.addComponent(predNumLayout);
        
        HorizontalLayout terminologyNumLayout = new HorizontalLayout();
        terminologyNumLayout.addComponent(new Label("Terminology count: \t"));
        terminologyNumLayout.addComponent(new Label(docDetail.getTerminologyCount().toString()));
        statistics.addComponent(terminologyNumLayout);
        
        HorizontalLayout uniqueTerminologyLayout = new HorizontalLayout();
        uniqueTerminologyLayout.addComponent(new Label("Unique terminology count: \t"));
        uniqueTerminologyLayout.addComponent(new Label(docDetail.getUniqueTerminologyCount().toString()));
        statistics.addComponent(uniqueTerminologyLayout);
        
        HorizontalLayout sentenceNumLayout = new HorizontalLayout();
        sentenceNumLayout.addComponent(new Label("Sentence count: \t"));
        sentenceNumLayout.addComponent(new Label(docDetail.getSentenceCount().toString()));
        statistics.addComponent(sentenceNumLayout);
        
        verticalLayout.addComponent(statistics);
		this.addComponent(verticalLayout);
        
        this.addComponent(new TripleTerminologyInfo().setDetails(docDetail));
        this.setSizeFull();
		return this;
	}

}


